############################
# Replication file: "Insiders, outsiders, skills and preferences for social protection: evidence from a survey experiment in Argentina"
# Irene Men�ndez Gonz�lez, January 2021
############################

# Figures survey experiment

library(readxl)

###### Figure 2(a): Treatment effects for insiders, outsiders and their difference

res <- read_excel("Fig2Estimates.xlsx", sheet="2a", col_names = TRUE)

coef.risk <- res$coef.risk
se.risk <- res$se.risk

coef.pov <- res$coef.pov
se.pov <- res$se.pov

#intersection of effects on y axis
yr <-  c(1.7, 1.5, 1.3)
yp <-  c(0.7, 0.5, 0.3)

t <- 1.96 #Critical t-value for 95% confidence interval

#Label for groups
group <- c("Poverty treatment", "Risk treatment")

#Commenting out printing of PDF figure
pdf("Fig2a.pdf")

#Parameters of plot region, to allow for wider left margin
par(mar=c(6.1,8.1,2.1,1.1))

#Call empty plot
plot(0,0,type="n", ylim=c(0,2), xlim=c(-0.6, 0.6), 
     yaxt="n", xlab="", ylab="", main = , axes=F)

#Add axes     
axis(1, cex.axis=1, tcl=-0.3)

axis(2, at= c(0.5, 1.5), labels=group, las=1, cex.axis=1, tick=F, line=-1.5, cex.lab=1.3)

mtext("Support for non-registered over registered workers", 1, line = 3, cex.lab=1.2)

abline(v=0, col="Black",lty=2, lwd=1)

#Add risk ATEs plus SEs (can do less then 95%)
points(coef.risk, yr, pch= c(19, 15, 17), cex=1.1)

segments(coef.risk -t*se.risk, yr, coef.risk + t*se.risk, yr, lwd=1.1)

#Same for poverty frame
points(coef.pov, yp, pch= c(19, 15, 17), cex=1.1)

segments(coef.pov - t*se.pov, yp, coef.pov + t*se.pov, yp, lwd=1.1)

#Grid lines
for(j in c(0.3, 0.5, 0.7, 1.3, 1.5, 1.7)){
lines(c(-0.6, 0.6), c(j,j), col = "lightgray", lty = "dotted", lwd=0.9)
}
for(i in seq(-0.6, 0.6, 0.1)){
abline(v=i, col = "lightgray", lty = "dotted", lwd=0.9)
}

#Legend

legend(x=-0.6, y = 2.04, pch= c(19, 15, 17), cex=0.9, legend = c("Insider (N=294)", "Outsider (N=434)", "Difference"))

dev.off()

####### Figure 2(b): Treatment effects for low- and high-skilled insiders, and their difference

res <- read_excel("Fig2Estimates.xlsx", sheet="2b", col_names = TRUE)

coef.risk <- res$coef.risk
se.risk <- res$se.risk

coef.pov <- res$coef.pov
se.pov <- res$se.pov

#intersection of effects on y axis
yr <-  c(1.7, 1.5, 1.3)
yp <-  c(0.7, 0.5, 0.3)

t <- 1.96 #Critical t-value for 95% confidence interval

#Label for groups
group <- c("Poverty treatment", "Risk treatment")

#Commenting out printing of PDF figure
pdf("Fig2b.pdf")

#Parameters of plot region, to allow for wider left margin
par(mar=c(6.1,8.1,2.1,1.1))

#Call empty plot
plot(0,0,type="n", ylim=c(0,2), xlim=c(-0.6, 0.6), 
     yaxt="n", xlab="", ylab="", main = , axes=F)

#Add axes     
axis(1, cex.axis=1, tcl=-0.3)

axis(2, at= c(0.5, 1.5), labels=group, las=1, cex.axis=1, tick=F, line=-1.5, cex.lab=1.3)

mtext("Support for non-registered over registered workers", 1, line = 3, cex.lab=1.2)

abline(v=0, col="Black",lty=2, lwd=1)

#Add risk ATEs plus SEs (can do less then 95%)
points(coef.risk, yr, pch= c(19, 15, 17), cex=1.1)

segments(coef.risk -t*se.risk, yr, coef.risk + t*se.risk, yr, lwd=1.1)

#Same for poverty frame
points(coef.pov, yp, pch= c(19, 15, 17), cex=1.1)

segments(coef.pov - t*se.pov, yp, coef.pov + t*se.pov, yp, lwd=1.1)

#Grid lines
for(j in c(0.3, 0.5, 0.7, 1.3, 1.5, 1.7)){
lines(c(-0.6, 0.6), c(j,j), col = "lightgray", lty = "dotted", lwd=0.9)
}
for(i in seq(-0.6, 0.6, 0.1)){
abline(v=i, col = "lightgray", lty = "dotted", lwd=0.9)
}

#Legend

legend(x=-0.6, y = 2.04, pch= c(19, 15, 17), cex=0.9, legend = c("Low-skill insider (N=194)", "High-skill insider (N=94)", "Difference"))

dev.off()


